﻿using Ops.Mpc.States;
using Ops.Mpc.ExampleApplication.DataPaging.Models;
using Ops.Mpc.Collections;
using System.Collections.Generic;
using Ops.Mpc.Filters;
using System.ComponentModel;
using Ops.Mpc.ExampleApplication.ExampleService;
using Ops.Mpc.Events;
using Ops.Mpc.ExampleApplication.Models;
using Ops.Mpc.Ioc;

namespace Ops.Mpc.ExampleApplication.DataPaging.Actions
{
    public class GetPersonPagerStateAction : StateAction
    {
        #region Fields
        private DataPagingModel _dataPagingModel = null;
        #endregion

        public override void Start()
        {
            base.Start();
            _dataPagingModel = this.GetModel<DataPagingModel>();
        }

        protected override void OnExecute(object context)
        {
            base.OnExecute(context);

           this.SetBusyMessage("Loading pager");

            List<Filter> filters = new List<Filter>();
            foreach (SortDescription sortDescription in _dataPagingModel.Items.SortDescriptions)
                filters.Add(new SortFilter() { PropertyName = sortDescription.PropertyName, SortAscending = sortDescription.Direction == ListSortDirection.Ascending });

            IocServiceLocator.Get<IExampleServiceContext>().GetPersons(filters.ToArray(), GetPersonsCompleted, OnError);

        }

        private void GetPersonsCompleted(Pager pager, object context)
        {

            this.Finish();

            pager.PageSize = 8;
            _dataPagingModel.Items.Pager = pager;


        }
    }
}
